using System.Security.Claims;
using CampusDataOperationLibrary;
using CampusSignInManageSystemWebAPI.Command.Teacher;
using CampusSignInManageSystemWebAPI.Responses.@public;
using CampusSignInManageSystemWebAPI.Responses.Teacher;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PublicProject.PublicClass.OutPutModels;
using PublicProject.PublicMethods;

namespace CampusSignInManageSystemWebAPI.Controllers;

[ApiController]
[Authorize]
[Route("[controller]/[action]")]
public class TeacherController(TeacherCourseOperation teacherCourseOperation) : ControllerBase
{

    [HttpGet]
    public async Task<OutputModel<List<OutputCourseModel>>> GetTeacherCourses()
    {
        var id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
        var result = await teacherCourseOperation.GetCoursesByTeacherIdAsync(id);
        var output = result.Select(x => new OutputCourseModel
        {
            CourseName = x.CourseName,
            TeacherName = x.Teacher.Name,
            NowSignInCount = x.Class.Students.Count,
            ClassName = x.Class.ClassName
        }).ToList();
        return OutPutMethod<List<OutputCourseModel>>.Success(output, message: "获取成功");
    }

    [HttpPost]
    public async Task<OutputModel<bool>> AddCourse([FromBody] InputCourseModel input)
    {
        var id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
        var result = await teacherCourseOperation.AddCourseAsync(id, input.CourseName, input.ClassName);
        return OutPutMethod<bool>.Success(result, message: "添加成功");
    }

    [HttpPost]
    public async Task<OutputModel<int>> AddAttendanceRecord([FromBody] InputAttendanceRecordModel input)
    {
        var id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
        var result = await teacherCourseOperation.AddAttendanceRecordAsync(id, input.CourseName, input.LayerId);
        return OutPutMethod<int>.Success(result, message: "创建签到记录成功");
    }

    [HttpGet]
    public async Task<OutputModel<AttendanceStatisticalList>> GetAttendanceStatisticalList()
    {
        var id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
        var queryResult = await teacherCourseOperation.GetAttendanceStatisticalListAsync(id);
        var result = new AttendanceStatisticalList
        {
            Courses = queryResult.Item1.Select(x => new OutCourse
            {
                CourseName = x.CourseName
            }).ToList(),
            Attendances = queryResult.Item2.Select(x => new List<OutAttendance>(
                x.Select(y => new OutAttendance
                {
                    courseName = y.Course.CourseName,
                    AttendanceID = y.AttendanceID,
                    Date = y.AttendanceTime.ToString("yyyy-MM-dd HH:mm:ss")
                })
            )).ToList()
        };
        return OutPutMethod<AttendanceStatisticalList>.Success(result, message: "获取成功");
    }

    [HttpGet]
    public async Task<OutputModel<CourseAttendanceDate>> GetCourseAttendanceDate(int attendanceId)
    {
        var queryResult = await teacherCourseOperation.GetCourseAttendanceDateAsync(attendanceId);
        var result = new CourseAttendanceDate
        {
            Attended = queryResult.Item1,
            Absent = queryResult.Item2,
            AbsentStudents = queryResult.Item3.Select(x => new AbsentStudent()
            {
                StudentName = x.Name
            }).ToList()
        };
        return OutPutMethod<CourseAttendanceDate>.Success(result, message: "获取成功");
    }
    
    [HttpDelete]
    public async Task<OutputModel<bool>> DeleteCourse([FromQuery] string courseName)
    {
        var id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
        var result = await teacherCourseOperation.DeleteCourseAsync(id, courseName);
        return OutPutMethod<bool>.Success(result, message: "删除成功");
    }
}